﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace AndroidEditor.WForm
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    public partial class EnvironmentSetup : Form
    {

        /// <summary>
        /// 
        /// </summary>
        private bool Destroy = false;
        /// <summary>
        /// Initializes a new instance of the <see cref="EnvironmentSetup"/> class.
        /// </summary>
        /// <remarks></remarks>
        public EnvironmentSetup()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the button1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != "" && textBox2.Text.Trim() != "")
            {

                this.DialogResult = DialogResult.OK;
            }
            else
            {
                textBox1.Focus();
            }
        }

        /// <summary>
        /// Handles the Click event of the button2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }



        /// <summary>
        /// Tests the directory for NDK.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private bool TestDirectoryForNDK(string path)
        {
            bool test = true;
            if (!File.Exists(path + @"\ndk-build"))
            {
                test = false;
            }
            if (!File.Exists(path + @"\ndk-build.cmd"))
            {
                test = false;
            }
            if (!Directory.Exists(path + @"\toolchains"))
            {
                test = false;
            }
            return test;
        }


        /// <summary>
        /// Handles the 1 event of the button3_Click control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button3_Click_1(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = @"c:\";

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (TestDirectoryForNDK(folderBrowserDialog1.SelectedPath))
                {
                    textBox1.Text = folderBrowserDialog1.SelectedPath;
                }
                if (TestDirectoryForCompiler(folderBrowserDialog1.SelectedPath + @"\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin"))
                {
                    textBox2.Text = folderBrowserDialog1.SelectedPath + @"\toolchains\arm-linux-androideabi-4.4.3\prebuilt\windows\bin";
                }
                else
                {
                    textBox2.Text = "";
                }
            }
        }

        /// <summary>
        /// Tests the directory for compiler.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private bool TestDirectoryForCompiler(string path)
        {
            if (File.Exists(path + @"\arm-linux-androideabi-gcc.exe") && File.Exists(path + @"\arm-linux-androideabi-g++.exe"))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Handles the Load event of the EnvironmentSetup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void EnvironmentSetup_Load(object sender, EventArgs e)
        {
           textBox1.Text= GlobleSetting.Instance().GetNDK();
           textBox2.Text = GlobleSetting.Instance().GetCompilerDIR();
           textBox1.Focus();
        }




        /// <summary>
        /// Froms the load.
        /// </summary>
        /// <remarks></remarks>
        public void fromLoad()
        {
            Destroy = true;
        }

        /// <summary>
        /// Handles the Shown event of the EnvironmentSetup control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void EnvironmentSetup_Shown(object sender, EventArgs e)
        {
            if (Destroy)
            {
                foreach (TabPage item in tabControl1.TabPages)
                {
                    if (item.Name != "tabPage4")
                    {
                        item.Dispose();
                    }
                    
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the button5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        /// <remarks></remarks>
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                folderBrowserDialog1.SelectedPath = textBox1.Text;
            }
            else
            {
                folderBrowserDialog1.SelectedPath  = @"c:\";

            }
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (TestDirectoryForCompiler(folderBrowserDialog1.SelectedPath))
                {
                    textBox2.Text = folderBrowserDialog1.SelectedPath.Trim();
                }
            }
        }

        /// <summary>
        /// Gs the get NDK path.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string gGetNdkPath()
        {
            return textBox1.Text.Trim();
        }

        /// <summary>
        /// Gets the compiler path.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public string GetCompilerPath()
        {
            return textBox2.Text.Trim();
        }
    }
}
